package com.xiaoma.myaudience.biz.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;

import com.xiaoma.myaudience.R;
import com.xiaoma.myaudience.biz.model.HomeInfoModel;
import com.xiaoma.myaudience.biz.model.RankingListModel;
import com.xiaoma.myaudience.biz.view.LoadingImageView;
import com.xiaoma.myaudience.util.PicUtils;
import com.xiaoma.myaudience.util.model.ModelUtils;

/**
 * 首页面
 * 
 * @author jpren
 * 
 */
public class HomeHotListAdapter extends BaseAdapter {

	private Context mContext;
	private List<Map<String, Object>> mList;
	private LayoutInflater mInflater;
	private String mSizeStr;
	//此字段是用来区分热图和新图，因为两者都用了同一个Adapter无法区分，在穿送type时候需要一个字段来标识
	private int mtype;

	public int getMtype() {
		return mtype;
	}

	public void setMtype(int mtype) {
		this.mtype = mtype;
	}

	public HomeHotListAdapter(Context context, List<Map<String, Object>> list,
			int type) {
		mContext = context;
		mList = list;
		mtype = type;
		mInflater = LayoutInflater.from(context);
		mSizeStr = PicUtils.composeSize((int) context.getResources()
				.getDimension(R.dimen.list_item_img_width), (int) context
				.getResources().getDimension(R.dimen.list_item_img_height));
	}

	@Override
	public int getCount() {
		return mList.size() + 1;
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (getCount() - 1 == position) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.more_horize_layout, null);
			Button mListMoreBtn = (Button) convertView
					.findViewById(R.id.list_more_btn);
		} else {
			convertView = mInflater.inflate(R.layout.home_hot_item, null);
			ViewHolder holder = new ViewHolder();
			holder.img = (LoadingImageView) convertView.findViewById(R.id.img);
			holder.score = (TextView) convertView.findViewById(R.id.score);
			holder.ratingBar = (RatingBar) convertView
					.findViewById(R.id.rating_bar);
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.zcr = (TextView) convertView.findViewById(R.id.zcr);
			holder.date = (TextView) convertView.findViewById(R.id.date);

			if (mtype == 2) {
				holder.score.setVisibility(View.GONE);
				holder.ratingBar.setVisibility(View.GONE);
			} else {
				// holder.score.setVisibility(View.VISIBLE);
				// holder.ratingBar.setVisibility(View.VISIBLE);
			}
			try {
				Map<String, Object> item = (Map<String, Object>) getItem(position);
				holder.img.setLoadingDrawable(
						ModelUtils.getStringValue(item, RankingListModel.PARAM_PATH),
						mSizeStr);
				String score = ModelUtils.getStringValue(item, HomeInfoModel.PARAM_HOT);
				holder.score.setText(score);
				double rating = TextUtils.isEmpty(score) ? 0 : Double.valueOf(score);
				holder.ratingBar.setRating((float) Math.floor(rating / 20));
				holder.title.setText(ModelUtils.getStringValue(item,
						RankingListModel.PARAM_TITLE));
				holder.zcr.setText(ModelUtils.getStringValue(item,
						RankingListModel.PARAM_USERNAME));
				holder.date.setText(ModelUtils.getStringValue(item,
						RankingListModel.PARAM_DATETIME).replaceAll("\\s+", ""));
				}catch (Exception e) {
					
				}
		}
		
		return convertView;
	}

	private class ViewHolder {
		LoadingImageView img;
		TextView score;
		RatingBar ratingBar;
		TextView title;
		TextView zcr;
		TextView date;
	}

}